//
//  AssEventTable.m
//  GrosoSub
//
//  Created by Josu López Fernández on 14/06/08.
//  Copyright 2008 Josu López Fernández. All rights reserved.
//	
//	Redistribution and use in source and binary forms, with or without
//	modification, are permitted provided that the following conditions are met:
//
//		* Redistributions of source code must retain the above copyright notice,
//		  this list of conditions and the following disclaimer.
//		* Redistributions in binary form must reproduce the above copyright
//		  notice, this list of conditions and the following disclaimer in the
//		  documentation and/or other materials provided with the distribution.
//		* Neither the name of the library nor the names of its
//		  contributors may be used to endorse or promote products derived from
//		  this software without specific prior written permission.
//
//  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
//  AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
//  IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
//  ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
//  LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
//  CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
//  SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
//  INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
//  CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
//  ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
//  POSSIBILITY OF SUCH DAMAGE.
//

#import "AssEventTable.h"


@implementation AssEventTable
	- (void)awakeFromNib
	{
		[lines setStringValue:[NSString stringWithFormat:@"%d",0]];
		[file setStringValue:@""];
	}

	- (void)dealloc
	{
		[table release];
		[list release];
		[super dealloc];
	}

	- (id)tableView:(NSTableView *)aTableView objectValueForTableColumn:(NSTableColumn *)aTableColumn row:(int)rowIndex
	{
		AssEvent *row;
		row = [list getAssEvent:rowIndex];
		NSString *ident = [aTableColumn identifier];
		if ([ident isEqualToString:@"type"]) {
			if ([row dialogue]) {
				return @"D";
			} else {
				return @"C";
			}
		} else if ([ident isEqualToString:@"line"]) {
			return [NSString stringWithFormat:@"%d", rowIndex + 1];
		} else if ([ident isEqualToString:@"layer"]) {
			return [NSString stringWithFormat:@"%d", [row layer]];
		} else if ([ident isEqualToString:@"start"]) {
			return [[row start] toString];
		} else if ([ident isEqualToString:@"end"]) {
			return [[row end] toString];
		} else if ([ident isEqualToString:@"style"]) {
			return [row style];
		} else if ([ident isEqualToString:@"name"]) {
			return [row name];
		} else if ([ident isEqualToString:@"marginL"]) {
			return [[row marginL] toString];
		} else if ([ident isEqualToString:@"marginR"]) {
			return [[row marginR] toString];
		} else if ([ident isEqualToString:@"marginV"]) {
			return [[row marginV] toString];
		} else if ([ident isEqualToString:@"effect"]) {
			return [row effect];
		} else if ([ident isEqualToString:@"text"]) {
			return [row text];
		}
		return nil;
	}
	
	- (int)numberOfRowsInTableView:(NSTableView *)aTableView
	{
		return [list assEvents];
	}
	
	- (void)tableView:(NSTableView *)aTableView setObjectValue:(id)anObject forTableColumn:(NSTableColumn *)aTableColumn row:(NSInteger)rowIndex
	{
		AssEvent *row = [list getAssEvent:rowIndex];
		NSString *ident = [aTableColumn identifier];
		if ([ident isEqualToString:@"type"]) {
			if ([anObject isEqualToString:@"D"]) {
				[row setDialogue:YES];
			} else if ([anObject isEqualToString:@"C"]) {
				[row setDialogue:NO];
			}
		} else if ([ident isEqualToString:@"layer"]) {
			[row setLayer:[anObject intValue]];
		} else if ([ident isEqualToString:@"start"]) {
			AssTime *at = [row start];
			[at fillFromString:anObject];
			[row setStart:at];
		} else if ([ident isEqualToString:@"end"]) {
			AssTime *at = [row end];
			[at fillFromString:anObject];
			[row setEnd:at];
		} else if ([ident isEqualToString:@"style"]) {
			[row setStyle:anObject];
		} else if ([ident isEqualToString:@"name"]) {
			[row setName:anObject];
		} else if ([ident isEqualToString:@"marginL"]) {
			AssMargin *am = [row marginL];
			[am fillFromString:anObject];
			[row setMarginL:am];
		} else if ([ident isEqualToString:@"marginR"]) {
			AssMargin *am = [row marginR];
			[am fillFromString:anObject];
			[row setMarginR:am];
		} else if ([ident isEqualToString:@"marginV"]) {
			AssMargin *am = [row marginV];
			[am fillFromString:anObject];
			[row setMarginV:am];
		} else if ([ident isEqualToString:@"effect"]) {
			[row setEffect:anObject];
		} else if ([ident isEqualToString:@"text"]) {
			[row setText:anObject];
		}
	}
	
	- (IBAction)insertAssEvent:(id)sender
	{
		AssEvent *newEvent = [AssEvent new];
		[list addAssEvent:newEvent atIndex:([table selectedRow] + 1)];
		[table reloadData];
		[lines setStringValue:[NSString stringWithFormat:@"%d",[list assEvents]]];
	}
	
	- (IBAction)deleteAssEvent:(id)sender
	{
		[list delAssEvent:[table selectedRow]];
		[table reloadData];
		[lines setStringValue:[NSString stringWithFormat:@"%d",[list assEvents]]];
	}
	
	- (IBAction)duplicateAssEvent:(id)sender{
		AssEvent *newEvent = [AssEvent new];
		[newEvent fillFromString:[[list getAssEvent:[table selectedRow]] toString]];
		[list addAssEvent:newEvent atIndex:([table selectedRow] + 1)];
		[table reloadData];
		[lines setStringValue:[NSString stringWithFormat:@"%d",[list assEvents]]];
	}
	
	- (IBAction)loadFromFile:(id)sender
	{
		NSArray *fileTypes = [NSArray arrayWithObjects:@"ass", @"ssa", nil];
		NSOpenPanel *oPanel = [NSOpenPanel openPanel];
		
		[oPanel setCanChooseFiles:YES];
		[oPanel setCanCreateDirectories:NO];
		[oPanel setCanCreateDirectories:NO];
		[oPanel setAllowsMultipleSelection:NO];
		[oPanel setTitle:@"Select a file to load"];
		
		if ([oPanel runModalForDirectory:nil file:nil types:fileTypes] == NSOKButton) {
			NSString *events, *event;
			events = @"";
			NSString *assFile = [NSString stringWithContentsOfFile:[oPanel filename] encoding:NSUTF8StringEncoding error:NULL];
			NSScanner *sc = [NSScanner scannerWithString:assFile];
			[sc scanUpToString:@"[Events]" intoString:NULL];
			[sc scanUpToString:@"\n" intoString:NULL];
			[sc scanString:@"\n" intoString:NULL];
			[sc scanUpToString:@"\n" intoString:NULL];
			[sc scanString:@"\n" intoString:NULL];
			while ([sc isAtEnd] == NO) {
				[sc scanUpToString:@"\n" intoString:&event];
				events = [events stringByAppendingString:event];
				events = [events stringByAppendingString:@"\n"];
			}
			list = [AssEventList new];
			[list fillFromString:events];
			[file setStringValue:[oPanel filename]];
			[lines setStringValue:[NSString stringWithFormat:@"%d",[list assEvents]]];
			[table reloadData];
		}
	}
	//- (IBAction)saveToFile:(id)sender;
	- (IBAction)closeFile:(id)sender
	{
		list = [list init];
		[table reloadData];
		[file setStringValue:@""];
		[lines setStringValue:[NSString stringWithFormat:@"%d",0]];
	}
@end
